<template>
  <el-dropdown style="margin-right:10px" @command="handleExport">
    <el-button size="small" icon="el-icon-upload2">
      导出
    </el-button>
    <el-dropdown-menu slot="dropdown">
      <el-dropdown-item command="all">导出全部数据</el-dropdown-item>
      <el-dropdown-item :disabled="multipleSelection.length===0" command="sel">导出选中数据</el-dropdown-item>
    </el-dropdown-menu>
  </el-dropdown>
</template>
<script>
import { excelAction } from '@/api/bid'
export default {
  props: {
    url: {
      type: String
    },
    multipleSelection: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
    }
  },
  methods: {
    handleExport(row) {
      if (row === 'all') {
        excelAction(this.url).then((res) => {
          const link = document.createElement('a')
          const blob = new Blob([res], { type: 'application/vnd.ms-excel' })
          link.style.display = 'none'
          link.href = URL.createObjectURL(blob)
          link.setAttribute('download', `导出文档.xls`)
          document.body.appendChild(link)
          link.click()
          document.body.removeChild(link)
        })
      } else {
        const ids = this.multipleSelection.map((i) => i.id).join(',')
        console.log(ids)
        excelAction(this.url, { ids }).then((res) => {
          const link = document.createElement('a')
          const blob = new Blob([res], { type: 'application/vnd.ms-excel' })
          link.style.display = 'none'
          link.href = URL.createObjectURL(blob)
          link.setAttribute('download', `导出文档.xls`)
          document.body.appendChild(link)
          link.click()
          document.body.removeChild(link)
        })
      }
    }
  }
}
</script>
